<?php
namespace app\user\controller;

use app\common\controller\UserBase;
use think\facade\Request;
use think\facade\Db;
use think\facade\Session;

use app\user\model\Customer;
use app\user\model\User;
use app\user\model\CustomerTrack;
use app\user\model\CustomerOrder;
use app\user\model\CustomerConfig;
use app\user\model\CustomerOrderClassify;

class Analysis extends UserBase
{
    //客户列表
    public function index()
    {
        //时间 - 默认本月
        if(input('dateframe')){
            $dateframe = input('dateframe');
            $start_time = strtotime(substr($dateframe,0,10));
            $end_time = strtotime(substr($dateframe,-10))+86399;
        }else{
            $month = date('Y-m',time());
            $start_time = strtotime($month);//指定月份月初时间戳  
            $end_time = mktime(23, 59, 59, date('m', strtotime($month))+1, 00);//指定月份月末时间戳
            $dateframe = date('Y-m-d',$start_time).' - '.date('Y-m-d',$end_time);
        }
        //基本信息
        $data['customer'] = Customer::where('user_id', UserId())->whereTime('create_time','between',[$start_time,$end_time])->count();
        $data['customer_all'] = Customer::where('user_id', UserId())->count();
        $data['track'] = CustomerTrack::where('user_id', UserId())->whereTime('create_time','between',[$start_time,$end_time])->count();
        $data['track_all'] = CustomerTrack::where('user_id', UserId())->count();
        $data['order'] = CustomerOrder::where('user_id', UserId())->whereTime('create_time','between',[$start_time,$end_time])->sum('price');
        $data['order_count'] = CustomerOrder::where('user_id', UserId())->whereTime('create_time','between',[$start_time,$end_time])->count();
        
        //最近30天
        //$start_time = mktime(0,0,0,date('m',strtotime('-30 days')),date('d',strtotime('-30 days')),date('Y',strtotime('-30 days')));
        //$end_time = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        $now_j = (($end_time-$start_time)/86400);
        $num = '0';
        for($i=0;$i<$now_j;$i++){
            //每日时间戳
            $day_begin = $start_time+$i*86400;
            $day_end = ($start_time+$i*86400)+86399;
            
            $date[] = date('d',$start_time+$i*86400); //每隔一天赋值给数组
            $month_data['date'][] = date('m-d',$start_time+$i*86400); //每隔一天赋值给数组
            $month_data['price'][] = CustomerOrder::whereTime('create_time', 'between', [$day_begin,$day_end])->sum('price');
        }
        
        return $this->fetch('index',[
            'dateframe'  => $dateframe,
            'data' => $data,
            'date'=>$date,
            'month_data'=>$month_data,
            'now_j' => $now_j
        ]);
    }
    
    //客户列表
    public function operation()
    {
        //时间 - 默认本月
        if(input('dateframe')){
            $dateframe = input('dateframe');
            $start_time = strtotime(substr($dateframe,0,10));
            $end_time = strtotime(substr($dateframe,-10))+86399;
        }else{
            $month = date('Y-m',time());
            $start_time = strtotime($month);//指定月份月初时间戳  
            $end_time = mktime(23, 59, 59, date('m', strtotime($month))+1, 00);//指定月份月末时间戳
            $dateframe = date('Y-m-d',$start_time).' - '.date('Y-m-d',$end_time);
        }
        //基本信息
        $data['customer'] = Customer::where('user_id', UserId())->whereTime('create_time','between',[$start_time,$end_time])->count();
        $data['customer_all'] = Customer::where('user_id', UserId())->count();
        $data['track'] = CustomerTrack::where('user_id', UserId())->whereTime('create_time','between',[$start_time,$end_time])->count();
        $data['track_all'] = CustomerTrack::where('user_id', UserId())->count();
        $data['order'] = CustomerOrder::where('user_id', UserId())->whereTime('create_time','between',[$start_time,$end_time])->sum('price');
        $data['order_count'] = CustomerOrder::where('user_id', UserId())->whereTime('create_time','between',[$start_time,$end_time])->count();
        
        //最近30天
        //$start_time = mktime(0,0,0,date('m',strtotime('-30 days')),date('d',strtotime('-30 days')),date('Y',strtotime('-30 days')));
        //$end_time = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        $now_j = (($end_time-$start_time)/86400);
        $num = '0';
        for($i=0;$i<$now_j;$i++){
            //每日时间戳
            $day_begin = $start_time+$i*86400;
            $day_end = ($start_time+$i*86400)+86399;
            
            $date[] = date('d',$start_time+$i*86400); //每隔一天赋值给数组
            $month_data['date'][] = date('m-d',$start_time+$i*86400); //每隔一天赋值给数组
            $month_data['customer'][] = Customer::whereTime('create_time', 'between', [$day_begin,$day_end])->count();
            $month_data['track'][] = CustomerTrack::whereTime('create_time', 'between', [$day_begin,$day_end])->count();
            $month_data['order'][] = CustomerOrder::whereTime('create_time', 'between', [$day_begin,$day_end])->count();
        }
        
        return $this->fetch('operation',[
            'dateframe'  => $dateframe,
            'data' => $data,
            'date'=>$date,
            'month_data'=>$month_data,
            'now_j' => $now_j
        ]);
    }
    //客户列表
    public function customer()
    {
        //时间 - 默认本月
        if(input('dateframe')){
            $dateframe = input('dateframe');
            $start_time = strtotime(substr($dateframe,0,10));
            $end_time = strtotime(substr($dateframe,-10))+86399;
        }else{
            $month = date('Y-m',time());
            $start_time = strtotime($month);//指定月份月初时间戳  
            $end_time = mktime(23, 59, 59, date('m', strtotime($month))+1, 00);//指定月份月末时间戳
            $dateframe = date('Y-m-d',$start_time).' - '.date('Y-m-d',$end_time);
        }
        //省份
        $province = Customer::withCount(['countProvince'])->where('user_id', UserId())->group('province')->select();
        $classify = CustomerConfig::withCount(['countClassify'])->where('field','classify')->order('sort_order desc,id desc')->select();
        $comefrom = CustomerConfig::withCount(['countComefrom'])->where('field','comefrom')->order('sort_order desc,id desc')->select();
        $status = CustomerConfig::withCount(['countStatus'])->where('field','status')->order('sort_order desc,id desc')->select();
        $order = CustomerOrderClassify::withCount(['countNum'])->withSum('countNum','price')->select();
        $user = User::withCount(['countNum'])->withSum('countPrice','price')->select();
        //$this->result($province);
        return $this->fetch('customer',[
            'dateframe'  => $dateframe,
            'province' => $province,
            'classify' => $classify,
            'comefrom' => $comefrom,
            'status' => $status,
            'order' => $order,
            'user' => $user,
        ]);
    }
    
    
}
